﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace VectorEditor
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}

		private void NewDocument()
		{
			if (documentView.IsModified)
			{
                var result = MessageBox.Show("Save changes to " + (string.IsNullOrEmpty(currentFilename) ? "untitled.vgd" : Path.GetFileName(currentFilename)),
                                "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    Save();
                }
                else if (result == System.Windows.Forms.DialogResult.Cancel)
                    return;
			}
			documentView.Document = new Document();
            currentFilename = "";
		}

		private void newToolStripMenuItem_Click(object sender, EventArgs e)
		{
			NewDocument();
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			NewDocument();
		}

        private void ellipseToolButton_Click(object sender, EventArgs e)
        {
            documentView.Tool = new EllipseTool();

			SwitchTool(sender);
        }

		private void SwitchTool(object sender)
		{
			foreach (var tool in toolBox.Items)
			{
				var button = (tool as ToolStripButton);
				if (button != null)
					button.Checked = false;
			}
			(sender as ToolStripButton).Checked = true;
		}

        private string currentFilename = null;

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Save();
        }

        private bool Save()
        {
            if (string.IsNullOrEmpty(currentFilename))
                return SaveAs();
            else
            {
                try
                {
                    documentView.Save(currentFilename);
                    return true;
                }
                catch (Exception)
                {
                    return SaveAs();
                }
            }
        }

        private bool SaveAs()
        {
            var dlg = new SaveFileDialog();
            dlg.DefaultExt = "vgd";
            dlg.Filter = "Vector Graphics Document|*.vgd|All Files|*.*";
            while (true)
            {
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    try
                    {
                        documentView.Document.Save(dlg.FileName);
                        currentFilename = dlg.FileName;
                        return true;
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Cannot save document to specified position. " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    return false;
                }
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveAs();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var dlg = new OpenFileDialog();
            dlg.DefaultExt = "vgd";
            dlg.Filter = "Vector Graphics Document|*.vgd|All Files|*.*";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    documentView.Document = Document.Open(dlg.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot open file. \n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private void editToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
        {
            undoToolStripMenuItem.Enabled = documentView.Controller.CanUndo;
            redoToolStripMenuItem.Enabled = documentView.Controller.CanRedo;
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            documentView.Controller.Undo();
        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            documentView.Controller.Redo();
        }

        private void editToolStripMenuItem_DropDownClosed(object sender, EventArgs e)
        {
            undoToolStripMenuItem.Enabled = true;
            redoToolStripMenuItem.Enabled = true;
        }

		private void pointerToolStripButton_Click(object sender, EventArgs e)
		{
			documentView.Tool = new PointerTool();

			SwitchTool(sender);
		}

	}
}
